<?php

/**
 * Hash (Associative Array) Operations
 * @package Nina
 * @author John Main <john@thedaddy.org>
 */
class Nina_Hash extends Nina_Array
{
	protected $hsh;

	/**
	 * Constructor
	 * @param array [$hsh=array()] The associative array (hash) to manipulate
	 */
	public function __construct($hsh = array()) {
		$this->hsh = $hsh;
	}
	
	/**
	 * Get the hash
	 * @return array
	 */
	public function get() {
		return $this->hsh;
	}

	/**
	 * Get the length of the hash
	 * @return integer
	 */
	public function length() {
		return count($this->hsh);
	}

	/**
	 * Check if the hash contains a key
	 * @param mixed $key The hash key
	 * @return boolean
	 */
	public function contains($key) {
		return array_key_exists($key, $this->hsh);
	}

	/**
	 * Search the hash for a value
	 * @param mixed $value The value to search for
	 * @return mixed
	 */
	public function search($value) {
		return array_search($value, $this->hsh);
	}

	/**
	 * Insert a key/value pair
	 * @param mixed $key The hash key
	 * @param mixed $value The value 
	 */
	public function insert($key, $value) {
		$this->hsh[$key] = $value;
	}

	/**
	 * Remove a key/value pair
	 * @param mixed $key The hash key
	 */
	public function remove($key) {
		unset($this->hsh[$key]);
	}

	/**
	 * Sort the hash by keys
	 * @param boolean [$forward=true] Sort it in a forwards direction
	 * @return array
	 */
	public function sort($forward = true) {
		$tmp_hsh = $this->hsh;
		if($forward) ksort($tmp_hsh);
		else krsort($tmp_hsh);
		return $tmp_hsh;
	}

	/**
	 * Merge a new hash into this one
	 * @param array $new_hsh The new hash
	 */
	public function merge($new_hsh) {
		$this->hsh = array_merge($this->hsh, $new_hsh);
	}

	/**
	 * Compute teh difference between this hash and another
	 * @param array $new_hsh The new hash
	 * @return array
	 */
	public function diff($new_hsh) {
		return array_diff_key($this->hsh, $new_hsh);
	}
}	